window.onload=function(){
	 init();
}
		var is_shake = 0,is_get = false,last_update=0,is_send =false;
		var SHAKE_THRESHOLD = 1500;  
		var x = y = z = last_x = last_y = last_z = 0;
		//音效
		var myaudio = document.getElementById('musicBox');
	    
		//获取是否中奖
	    function ajax_prize()
		  {
			
//			  var url = "index.php?g=mobile&m=long&a=ajaxPrize";
//				$.post(url,"",function(data){
//			         if(data.error==0){
//			        	 //中奖
//			        	 $("#prize_name").html(data.data.prize_name);
//			        	 if(data.is_saved == "true"){
//			        		 $(".phone").hide();
//			        		 $(".phone").parent().attr("style","height:2.5rem");
//			        	 }else{
//			        		 $(".phone").show();
//			        		 $(".phone").parent().attr("style","");
//			        	 }
//			        	 document.getElementById('yao').className='yaoicon move'; 
//				            setTimeout(function(){
//					            $(".yaobox").hide();
//					            $(".winning").show();
//				            }, 1500);
//			         }else if(data.error == 1){
			        	 //不中奖
			        	  document.getElementById('yao').className='yaoicon move'; 
			        	 setTimeout(function(){
			        		 $(".tis").show();
				            }, 1500);
//			         }else{
//			        	 warn(data.content);
//			         }
//				},"json");
		  }
	    
		function getCards(){
			if(is_get){
				warn("领取卡券中...");
				return ;
			}
			if(is_send){
				warn("领取卡券中...");
				return ;
			}
			is_get = true;
			var tel = $("input[name='telphone']").val();
			var data = {
					tel:tel
			}
			var url = "index.php?g=mobile&m=long&a=getCards";
			$.post(url,data,function(result){
				is_get = false;
				if(result.error == 0){
					//中卡券
					warn(result.content);
					is_send = true;
					 wx.addCard({
			              cardList: result.cardlist,
			              success: function (res) {
			            	  get_card(result.user_id);  
			              }
			            });
				}else{
					warn(result.content);
				}
			},"json")
		}
		function init() {  
		    
		    if (window.DeviceMotionEvent) {  
		        window.addEventListener('devicemotion', deviceMotionHandler, false);  
		       
		    } else {  
		        alert('not support mobile event');  
		    }  
		}  
		function deviceMotionHandler(eventData) {  
		    var acceleration = eventData.accelerationIncludingGravity;  
		    var curTime = new Date().getTime();  
		    if ((curTime - last_update) > 100) {  
		        var diffTime = curTime - last_update;  
		        last_update = curTime;  
		        x = acceleration.x;  
		        y = acceleration.y;  
		        z = acceleration.z;  
		        var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;  

		        if (speed > SHAKE_THRESHOLD ) { 
		        	
		        	if(is_shake==0)
		        	{
		        		
				        	 ajax_prize();
				        	 is_shake =1;
				        	        
		        	}
		        	 myaudio.play();
			         setTimeout("myaudio.pause()",1200);   
		            
		        }else {
		            
		        }
		        last_x = x;  
		        last_y = y;  
		        last_z = z;  
		    }  
		};
		
		//领取卡券后异步回调
		  function get_card(id)
		  {
				  
			  var url = "index.php?g=mobile&m=long&a=saveCard";
				
			  var data = {
					  id:id
			  }
				$.post(url,data,function(res){
					is_send = false;
				},"text");
        	  
          
		  }
		//不打断alert
		  function warn(msg,opt,left,top){
		  	if(opt){
		  	 	var obj = $("#"+opt);
		  	}
		   	new Toast({context:$('body'),message:msg},obj,left,top).show();
		  }
		  var Toast = function(config,obj,left,top){
		  	this.context = config.context==null?$('body'):config.context;//上下文
		  	this.message = config.message;//显示内容
		  	this.time = config.time==null?5000:config.time;//持续时间
		  	this.left = config.left;//距容器左边的距离
		  	this.top = ($(document).height()/4)*3;//距容器上方的距离
		  	if(obj){ 
		  	this.left = obj.offset().left + left;
		  	this.top = obj.offset().top + top;
		  	}
		  	this.init();
		  }
		  var msgEntity;
		  Toast.prototype = {
		  	//初始化显示的位置内容等
		  	init : function(){
		  		$("#toastMessage").remove();
		  		//设置消息体
		  		var msgDIV = new Array();
		  		msgDIV.push('<div id="toastMessage">');
		  		msgDIV.push('<span>'+this.message+'</span>');
		  		msgDIV.push('</div>');
		  		msgEntity = $(msgDIV.join('')).appendTo(this.context);
		  		//设置消息样式
		  		var left = this.left == null ? this.context.width()/2-17-msgEntity.find('span').width()/2 : this.left;
		  		var top = this.top == null ? '20px' : this.top;
		  		msgEntity.css({position:'absolute',top:top,'z-index':'999',left:left,'background-color':'rgb(254, 222, 43)',color:'rgb(193, 43, 43)','font-size':'14px',padding:'5px 12px',margin:'5px','border-radius':'4px','-moz-border-radius':'4px','-webkit-border-radius':'4px',opacity:'0.6','font-family':'微软雅黑'});
		  		//msgEntity.addClass(".toast");
		  		msgEntity.hide();
		   	},
		  	//显示动画
		  	show :function(){
		  		msgEntity.fadeIn(this.time/2);
		  		msgEntity.fadeOut(this.time/2);
		  	}
		  }